#include "qvarproperty.h"
#include "qstringproperty.h"
#include "qfloatproperty.h"
#include "qintproperty.h"
#include "qcommonstruct.h"
#include "qabstractproperty.h"
#include "qpropertyfactory.h"
#include "xmlobject.h"
QVarProperty::QVarProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    setObjectProperty("type", "Var");
    m_dataType = "String";
    QVariant m_value;
}

QVariant QVarProperty::getProValue(){
    return m_value;
}


void QVarProperty::fromObject(XMLObject *xml)
{
    if(xml != NULL) {
        if(xml->getTagName() != PROPERTY_TITLE) {
            return;
        }

        QMapIterator<QString, QString> it(xml->getPropertys());
        while(it.hasNext()) {
            it.next();
            qDebug() << "QAbstractProperty::fromObject:Key->" << it.key() << ",value->" << it.value();
            m_propertys.insert(it.key(), it.value());
        }

        QList<XMLObject*> children = xml->getChildren();
        foreach(XMLObject* obj, children) {
            qDebug() << "QVarProperty::fromObject:children xml->" << obj->write();
            QString varName = obj->getProperty("name");
            QString varType = obj->getProperty("type");
            QAbstractProperty *pro = getChild(varName);
            if(pro != NULL) {
                pro->fromObject(obj);
                if(varName=="varType"){
                    m_varType = dynamic_cast<QEnumProperty*>(pro);
                }else if(varName=="varTag"){
                    m_varTag =  dynamic_cast<QTagProperty*>(pro);
                }else if(varName=="varValue"){
                    m_varValue =pro;
                }
            }else{
                QString errStr=QString("QVarProperty::fromObject:property:%1 has no exist property type %2!").arg(varName).arg(varType);
                qDebug() << errStr;
            }
        }
        //整理VarProperty，上面初始化，下面判断需要哪一个属性
        if(m_varType->getValue().toInt()==1){
            m_varValue->removePropertyFromParent();
        }else if(m_varType->getValue().toInt()==0){
            m_varTag->removePropertyFromParent();
        }
        //makeValue();
        emit refresh();
    }
}


void QVarProperty::setDataType(QString dataType){
    m_dataType = dataType;
    initChildProperties();
}

/**
 * @brief QVarProperty::initChildProperties
 * 在属性里设置变量m_dataType
 * 设置完后，需要重新初始化子节点属性
 */
void QVarProperty::initChildProperties(){
    //初始化VarType
    m_varType = new QEnumProperty(this);
    ComboItems items;
    tagComboItem item0;
    item0.m_text = tr("请选择变量类型.."); // 手工输入属性值
    item0.m_value = "-1";
    items.append(item0);
    tagComboItem item1;
    item1.m_text = tr("手工输入"); // 手工输入属性值
    item1.m_value = "0";
    items.append(item1);
    tagComboItem item2;
    item2.m_text = tr("动态数据绑定"); // 属性值从实时数据获取
    item2.m_value = "1";
    items.append(item2);
    QVariant varType;
    varType.setValue(items);
    m_varType->setAttribute("show_name", tr("参数类型")); // tr("x")
    m_varType->setObjectProperty("name", "varType");
    m_varType->setAttribute("items",varType);
    connect(m_varType,&QEnumProperty::value_chaged,this,&QVarProperty::child_value_changed);
    /**
     * 这两个子对象在这里全部初始化，然后在fromObject时，视情况移除不需要的子对象。
     */
    //初始化varTag
    qDebug() << "QVarProperty::initChildProperties:dataType->" << m_dataType;
    m_varTag = dynamic_cast<QTagProperty*>(QPropertyFactory::create_property("Tag"));
    m_varTag->setObjectProperty("name", "varTag");
    m_varTag->setAttribute("show_name", tr("绑定变量")); // tr("y")
    m_varTag->setAttribute("items",this->getAttribute("items"));//传递变量集数据
    m_varTag->setPropertyParent(this);
    connect(m_varTag,&QTagProperty::value_chaged,this,&QVarProperty::child_value_changed);
    //初始化varValue
    m_varValue = QPropertyFactory::create_property(m_dataType);
    m_varValue->setObjectProperty("name", "varValue");
    m_varValue->setAttribute("show_name", tr("值")); // tr("y")
    m_varValue->setAttribute(ATTR_EDITABLE, true);
    m_varValue->setPropertyParent(this);
    connect(m_varValue,&QStringProperty::value_chaged,this,&QVarProperty::child_value_changed);
}

QString QVarProperty::getDataType(){
    return m_dataType;
}

void QVarProperty::setValue(const QVariant &value)
{
    disconnect_children();
    qDebug() << "QVarProperty::setValue:value->" << value.toString();
    QVariantHash varHash = value.toHash();
    if(m_varType&&varHash.contains("varType")) m_varType->setValue(varHash.value("varType"));
    if(m_varTag&&varHash.contains("varTag")) m_varTag->setValue(varHash.value("varTag"));
    if(m_varValue&&varHash.contains("varValue")) m_varValue->setValue(varHash.value("varValue"));
    this->makeValue();
    QAbstractProperty::setValue(value);
    connect_children();
}

void QVarProperty::child_value_changed(const QVariant &oldValue, const QVariant &newValue,QString proName)
{
    qDebug() << "QVarProperty::child_value_changed: proName-> " << proName;
    qDebug() << "QVarProperty::child_value_changed: newValue-> " << newValue.toString();
    qDebug() << "QVarProperty::child_value_changed: dataType-> " << m_dataType;
    //设置参数临时存储，便于在后面setValue中获取
    this->setProperty(proName.toUtf8(),newValue);
    //重置子节点
    //if(m_varTag) m_varTag->removePropertyFromParent();
    //if(m_varValue) m_varValue->removePropertyFromParent();
    if(proName=="varType"){
        if(newValue.toInt()==1){
            //初始化varTag
            if(m_varValue) m_varValue->removePropertyFromParent();
            m_varTag = dynamic_cast<QTagProperty*>(QPropertyFactory::create_property("Tag"));
            m_varTag->setAttribute("show_name", tr("绑定变量")); // tr("y")
            m_varTag->setAttribute("items",this->getAttribute("items"));//传递变量集数据
            m_varTag->setObjectProperty("name", "varTag");
            m_varTag->setPropertyParent(this);
            connect(m_varTag,&QTagProperty::value_chaged,this,&QVarProperty::child_value_changed);
        }else if(newValue.toInt()==0){
            if(m_varTag) m_varTag->removePropertyFromParent();
            m_varValue = QPropertyFactory::create_property(m_dataType);
            m_varValue->setAttribute("show_name", tr("值")); // tr("y")
            m_varValue->setAttribute(ATTR_EDITABLE, true);
            m_varValue->setObjectProperty("name", "varValue");
            m_varValue->setPropertyParent(this);
            connect(m_varValue,&QStringProperty::value_chaged,this,&QVarProperty::child_value_changed);
        }
    }else if(proName=="varTag"){
        qDebug() << " QVarProperty::child_value_changed->" << proName;
    }else if(proName=="varValue"){
        qDebug() << " QVarProperty::child_value_changed->" << proName;
    }
    makeValue();
    setValue(m_value);
    //emit refresh();
}

QString QVarProperty::getValueText()
{
    if(m_varType->getValue().toInt()==1){
        if(m_varTag&&!m_varTag->getValue().isNull()){
            return QString("%1(%2)").arg(tr("RealData")).arg(m_varTag->getValue().toString());
        }else{
            return QString("%1(%2)").arg("RealData").arg("");
        }

    }else{
        if(m_varValue&&!m_varValue->getValue().isNull()){
            return QString("%1(%2)").arg("InputData").arg(m_varValue->getValue().toString());
        }else{
            return QString("%1(%2)").arg("InputData").arg("");
        }
    }
}

QIcon QVarProperty::getValueIcon()
{
    return QIcon();
}

void QVarProperty::makeValue()
{
    QVariantHash varHash;
    if(m_varType!=NULL&&!m_varType->getValue().isNull()){
        varHash.remove("varType");
        varHash.insert("varType",m_varType->getValue());
    }
    if(m_varType->getValue().toInt()==1){
        if(m_varTag->isWidgetType()&&!m_varTag->getValue().isNull()){
            qDebug() << "QVarProperty::makeValue->" << m_varTag->getValue();
            varHash.remove("varTag");
            varHash.insert("varTag",m_varTag->getValue());
        }
    }else{
        if(m_varValue->isWidgetType()&&!m_varValue->getValue().isNull())
        {
            varHash.remove("varValue");
            varHash.insert("varValue",m_varValue->getValue());
        }
    }
    m_value.setValue(varHash);
}

void QVarProperty::writeValue()
{

}


